Imports System.IO
Imports Cdm.DataCore.Entities
Imports System.Collections.Generic

Partial Class Case_caseSupportBroker
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Public Property SupportBrokerId() As Integer
        Get
            Try
                Dim pg As IPermission = Me.Page
                Dim pt As Participant = pg.PrimaryObject
                Return pt.SupportBrokerId
            Catch ex As Exception
                Return 0
            End Try
        End Get
        Set(ByVal value As Integer)
            Dim pg As IPermission = Me.Page
            Dim pt As Participant = pg.PrimaryObject
            pt.SupportBrokerId = value
            BindAgencies()
            Dim agencyId As Integer = GetAgencyId(value)
            Me.AgencyInputControl.Value = agencyId.ToString()
            BindBrokers()
            Me.BrokerInputControl.Value = value.ToString()
            SetFormViews()
            SetDefaults()
            Dim supportBrokerTitle As String = EntityController.GetSingleDisplayName("Support Broker")
            Me.SupportBrokerNameLabel.Text = supportBrokerTitle
            Me.BrokerInputControl.Title = supportBrokerTitle
            Dim lblInfo As Label = Me.BrokerFormView.FindControl("SupportBrokerInfoLabel")
            If Not lblInfo Is Nothing Then
                lblInfo.Text = supportBrokerTitle & " Info"
            End If
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Me.SupportBrokerId > 0 Then
            Dim supportBroker As Cdm.DataCore.Entities.SupportBroker = Cdm.DataCore.Entities.SupportBroker.Retrieve(Me.SupportBrokerId)
            If Me.AddInfoFileExists(supportBroker) Then
                Session("AddInfoPath") = supportBroker.AdditionalInfoFile
            Else
                Me.AddInfoLinkButton.Visible = False
            End If
        End If

    End Sub

    Protected Sub AddInfoLinkButton_Click(ByVal sender As Object, _
                                          ByVal e As System.EventArgs) _
                                          Handles AddInfoLinkButton.Click

        Dim file As New FileInfo(Session("AddInfoPath"))
        Response.Clear()
        'now we send the file header minus the resources extension.
        Response.AddHeader("Content-Disposition", "attachment; filename=" & _
             Replace(file.Name, ".resources", ""))
        Response.AddHeader("Content-Length", file.Length.ToString())
        Response.ContentType = "application/octet-stream"
        Response.WriteFile(file.FullName)
        Response.End()

    End Sub

    Protected Function AddInfoFileExists(ByVal supportBroker As Cdm.DataCore.Entities.SupportBroker) As Boolean

        If supportBroker.AdditionalInfoFile <> "" Then
            Dim file As New FileInfo(supportBroker.AdditionalInfoFile)
            If file.Exists Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If

    End Function

    ''' <summary>
    ''' Sets the supportbroker without binding the other items
    ''' </summary>
    ''' <param name="newId"></param>
    ''' <remarks></remarks>
    Private Sub SetSupportBroker(ByVal newId As Integer)

        Dim pg As IPermission = Me.Page
        Dim pt As Participant = pg.PrimaryObject
        pt.SupportBrokerId = newId
        Me.BrokerInputControl.Value = newId.ToString()
        SetBrokerFormView()

    End Sub

    Private Function GetAgencyId(ByVal supportBrokerId As Integer) As Integer
        If supportBrokerId = 0 Then
            Return 0
        End If
        Dim selectedSupportBroker As SupportBroker = SupportBroker.Retrieve(supportBrokerId)
        If selectedSupportBroker Is Nothing Then
            Return 0
        End If
        Return selectedSupportBroker.SupportBrokerAgencyId
    End Function

#Region "data sources"
    Private ReadOnly Property SupportBrokerAgencies() As List(Of SupportBrokerAgency)
        Get
            Dim lst As List(Of SupportBrokerAgency) = SupportBrokerAgency.ListByUser(UserAuthorization.CurrentUser)
            Return lst
        End Get
    End Property

    Private ReadOnly Property SupportBrokers() As List(Of SupportBroker)
        Get
            If Me.AgencyInputControl.Value Is Nothing Then
                Return Nothing
            ElseIf Me.AgencyInputControl.Value.Equals("") Then
                Return Nothing
            Else
                Dim lst As List(Of SupportBroker) = SupportBroker.ListByAgency(Convert.ToInt32(Me.AgencyInputControl.Value))
                Return lst
            End If

        End Get
    End Property

    Private ReadOnly Property SelectedSupportBroker() As List(Of SupportBroker)
        Get
            Try
                Dim i As Integer
                For i = 0 To Me.SupportBrokers.Count - 1
                    If SupportBrokers(i).SupportBrokerId = Me.SupportBrokerId Then
                        Dim lst As New List(Of SupportBroker)
                        lst.Add(Me.SupportBrokers(i))
                        Return lst
                    End If
                Next
                Return Nothing
            Catch ex As Exception
                Return Nothing
            End Try
        End Get
    End Property

    Private ReadOnly Property SelectedAgency() As List(Of SupportBrokerAgency)
        Get
            Dim i As Integer = Me.AgencyInputControl.SelectedIndex
            Dim id As Integer
            Dim lst As New List(Of SupportBrokerAgency)
            If i > -1 Then
                lst.Add(Me.SupportBrokerAgencies(i))
            Else
                For j As Integer = 0 To Me.SupportBrokerAgencies.Count
                    id = CInt(Me.AgencyInputControl.Value)
                    If id = Me.SupportBrokerAgencies(j).SupportBrokerAgencyId Then
                        lst.Add(Me.SupportBrokerAgencies(j))
                        Return lst
                    End If
                Next
            End If

            Return lst
        End Get
    End Property

#End Region

    Private Sub BindControls()
        BindAgencies()
        BindBrokers()
    End Sub

    Private Sub BindAgencies()

        If Me.AgencyInputControl.Items.Count > 0 Then Exit Sub
        Me.AgencyInputControl.DataSource = Me.SupportBrokerAgencies
        Me.AgencyInputControl.DataTextField = "AgencyName"
        Me.AgencyInputControl.DataValueField = "SupportBrokerAgencyId"
        Me.AgencyInputControl.DataBind()

    End Sub

    Private Overloads Sub BindBrokers()
        Me.BrokerInputControl.DataSource = Me.SupportBrokers
        Me.BrokerInputControl.DataTextField = "DisplayLastNameFirst"
        Me.BrokerInputControl.DataValueField = "SupportBrokerId"
        Me.BrokerInputControl.DataBind()
        If Me.BrokerInputControl.Editable = True Then
            Me.BrokerInputControl.Items.Insert(0, New ListItem("Select...", "0"))
            Me.BrokerInputControl.SelectedIndex = 0
        End If
    End Sub

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseSupportBroker
        End Get
    End Property

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Dim pg As IPermission
            pg = Me.Page
            Return pg.PrimaryObject
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            BindControls()
            Dim isNew As Boolean = False
            Dim pt As Participant = value
            If pt.ParticipantId = 0 Then isNew = True
            Me.SupportBrokerId = pt.SupportBrokerId
            SetDefaults()
        End Set
    End Property

    Private Sub SetDefaults()

        Me.BrokerInputControl.Editable = UserAuthorization.HasUpdatePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseSupportBroker)
        Me.AgencyInputControl.Editable = UserAuthorization.HasUpdatePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseSupportBroker)

        Select Case UserAuthorization.CurrentUser.RoleId
            Case Cdm.DataCore.UserSecurity.Role.RoleType.SupportBroker, _
                 Cdm.DataCore.UserSecurity.Role.RoleType.SupportBrokerAdmin
                If Not Me.Page.IsPostBack Then
                    Dim pg As IPermission = Me.Page
                    Dim pt As Cdm.DataCore.Entities.Participant = pg.PrimaryObject
                    If pt.ParticipantId = 0 And Me.SupportBrokerId = 0 Then
                        Dim sb As SupportBroker = Cdm.DataCore.Entities.SupportBroker.RetrieveByUserId(UserAuthorization.CurrentUser.UserId)
                        Me.AgencyInputControl.Value = sb.SupportBrokerAgencyId.ToString()
                        
                        BindBrokers()

                        SetSupportBroker(sb.SupportBrokerId)

                    End If
                End If

                Me.AgencyInputControl.Enabled = False
                If Me.AgencyInputControl.Value = "0" Then
                    Me.BrokerInputControl.Editable = False
                    'Me.ddlBroker.Visible = False
                End If
            Case Else
                'Exit Sub
        End Select

        If Me.BrokerInputControl.Editable = False Then
            Me.BrokerInputControl.cssClassControl = "label"
        End If
        If Me.AgencyInputControl.Editable = False Then
            Me.AgencyInputControl.cssClassControl = "label"
        End If

        'If dropdowns are not editable and enabled, hide the GO buttons
        If Me.AgencyInputControl.Editable = False Or Me.AgencyInputControl.Enabled = False Then
            Me.AgencySubmitButton.Visible = False
        Else
            Me.AgencySubmitButton.Visible = True
        End If
        If Me.BrokerInputControl.Editable = False Or Me.BrokerInputControl.Enabled = False Then
            Me.BrokerSubmitButton.Visible = False
        Else
            Me.BrokerSubmitButton.Visible = True
        End If
    End Sub

    Public Sub ResetControls() Implements IPermission.ResetControls

    End Sub

    Private Sub SetFormViews()
        SetAgencyFormView()
        SetBrokerFormView()
    End Sub

    Private Sub SetAgencyFormView()
        Dim showAgencyDetails As Boolean = False

        If Not (Me.AgencyInputControl.Value.Equals("0") Or Me.AgencyInputControl.Value.Equals("")) Then
            showAgencyDetails = True
        End If


        'If Agency is selected, bind and show the agency formview
        If showAgencyDetails = True Then
            Me.AgencyFormView.DataSource = Me.SelectedAgency
            Me.AgencyFormView.DataBind()
            Me.AgencyFormView.Visible = True
        Else
            'If Agency is not selected, hide the formview
            Me.AgencyFormView.Visible = False
        End If
    End Sub

    Private Sub SetBrokerFormView()
        If Me.BrokerInputControl.SelectedIndex > 0 Then
            Me.BrokerFormView.DataSource = Me.SelectedSupportBroker
            Me.BrokerFormView.DataBind()
            Me.BrokerFormView.Visible = True
        ElseIf Me.BrokerInputControl.SelectedIndex = 0 Then
            Me.BrokerFormView.Visible = False
        End If
    End Sub

    Protected Sub AgencyInputControl_SelectedIndexChanged() Handles AgencyInputControl.SelectedIndexChanged
        BindBrokers()
        SetFormViews()
    End Sub

    Protected Sub BrokerInputControl_SelectedIndexChanged() Handles BrokerInputControl.SelectedIndexChanged
        SetSupportBroker(Convert.ToInt32(Me.BrokerInputControl.Value))
        SetBrokerFormView()
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If Me.IsPostBack Then
            BindAgencies()
            If Me.SupportBrokerId > 0 Then
                Me.AgencyInputControl.Value = GetAgencyId(Me.SupportBrokerId).ToString()
                BindBrokers()
                Me.BrokerInputControl.Value = Me.SupportBrokerId.ToString()
            End If
            SetDefaults()
            If Not (Me.AgencyInputControl.Value Is Nothing) Then
                If Not (Me.AgencyInputControl.Value.Equals("")) Then
                    SetAgencyFormView()
                End If
            End If
        End If
    End Sub

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return EntityController.GetSingleDisplayName("Support Broker")
        End Get
    End Property

End Class
